package com.qdu.servlet;

import com.qdu.dao.ActiSingUpDao;
import com.qdu.entity.ActiSingUp;
import com.qdu.service.ActivityService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/signUp")
public class SignUpServlet extends HttpServlet {

    ActivityService activityService = new ActivityService();
    ActiSingUpDao actiSingUpDao=new ActiSingUpDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户id，假如用户id存在就说明登录了。假如为空，没有登录转到登录页面
        HttpSession session = req.getSession();
        String uid = null;
        uid = (String) session.getAttribute("PageUid");

        //没登陆
        if (uid == null) {
            resp.sendRedirect("Userlogin.html");
        } else {
            //登录了，获得活动编号
            String activityId = req.getParameter("Aid");
            ActiSingUp ifSigned=null;
            //判断是否为空
            ifSigned=actiSingUpDao.IfSigned(uid, activityId);
            //返回值为空，说明这个人还没有报名
            if(ifSigned==null){
                //值为空，没有报名
                activityService.signUp(activityId, uid);
                req.setAttribute("signUpUid", uid);
                req.setAttribute("signUpActivityId", activityId);
                req.getRequestDispatcher("/signSuccess.jsp").forward(req, resp);
            }
            else{
                //报名过
                req.setAttribute("signUpActivityId", activityId);
                req.getRequestDispatcher("/signSuccess.jsp").forward(req, resp);
            }
            
        }
    }

}
